var path = require('path');
var fs = require('fs');
var write = require('hy5b/backend/write');
var config = require('./config.json');

var argvs = process.argv;
if (argvs.length == 4) {
    var className = argvs[2];
    var moduleName = argvs[3];
    var pathName = [className, moduleName].join('/');
    
    fs.stat(path.resolve(pathName), function(err, stat) {
        if (err) {
            config[className] = config[className] || [];
            config[className].push(moduleName);

            write(path.resolve(pathName, 'js', 'app.js'))
                .write("define(function(require, exports, module) {\n\n  // var Backbone = require('backbone');\n  var $ = require('jquery');\n  // var _ = require('underscore');\n  // var jsonview = require('jsonview');\n  var tmpl = require('text!../tmpl.html');\n\n  function main() {\n    var $app = $('<div id=\"app\">');\n    $('#main').html($app);\n\n    $app.append(tmpl);\n  }\n\n  return main;\n})\n");
            write(path.resolve(pathName, 'tmpl.html'))
                .write('<style>\n\n</style>\n');
            
            write('config.json')
                .write(JSON.stringify(config, null, 2));
        } else {
            console.log('the module ' + pathName + ' has been exist.');
        }
    });

} else {
    console.log("Usage: " + path.basename(argvs[0]) + " " + path.basename(argvs[1]) + " {class name} {module name}");
    console.log('');
    for (var param in config) {
        console.log('  ', param);
        for (var i = 0; i < config[param].length; i++) {
            console.log('      ', config[param][i]);
        }
    }
}